﻿using NLog.Extensions.Logging;
using PLCS.Application;
using PLCS.Application.Contracts;
using PLCS.Domain.Shared;
using PLCS.EntityFrameworkCore.EntityFrameworkCore;
using PLCS.EquipmentService;
using PLCS.PlcInteraction;
using PLCS.SocialHttpApi;
using PLCS.SocialHttpClient;
using Volo.Abp.AspNetCore.ExceptionHandling;
using Volo.Abp.Autofac;

namespace PLCS.Web;

[DependsOn(typeof(AbpAspNetCoreMvcModule))]
[DependsOn(typeof(AbpAutofacModule))]
[DependsOn(typeof(PLCSApplicationContractsModule))]
[DependsOn(typeof(PLCSDomainSharedModule))]
[DependsOn(typeof(PLCSApplicationModule))]
[DependsOn(typeof(PLCSEntityFrameworkCoreModule))]
[DependsOn(typeof(EquipmentServiceModule))]
[DependsOn(typeof(PlcInteractionModule))]
[DependsOn(typeof(PLCSSocialHttpApiModule))]
[DependsOn(typeof(PLCSSocialHttpClientModule))]
public class PLCSWebModule : AbpModule
{
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        context.Services.AddEndpointsApiExplorer();
        //var cors = new EnableCorsAttribute("*", "*", "*");
        //GlobalConfiguration.Configuration.EnableCors(cors);

        context.Services.AddSwaggerGen(options =>
        {
            //options.SwaggerDoc("v1", new OpenApiInfo { Title = "动态API", Version = "v1" });
            options.DocInclusionPredicate((docName, description) => true);
            //options.CustomSchemaIds(type => type.FullName);
        });

        Configure<AbpAspNetCoreMvcOptions>(options =>
        {
            options
                .ConventionalControllers
                .Create(typeof(PLCSApplicationModule).Assembly, opts =>
                {
                    opts.UseV3UrlStyle = true;
                });
        });

        context.Services.AddMvc(option =>
        {
            //option.Filters.Add<RequestAuditFilter>(); //全局注册：
        });

        //调试用，显示错误信息的
        context.Services.Configure<AbpExceptionHandlingOptions>(options =>
        {
            options.SendExceptionsDetailsToClients = true;
        });

        context.Services.AddLogging(builder =>
        {
            builder.AddNLog();
        });
    }

    public override void OnApplicationInitialization(ApplicationInitializationContext context)
    {
        var app = context.GetApplicationBuilder();
        var env = context.GetEnvironment();
        // Configure the HTTP request pipeline.
        if (env.IsDevelopment())
        {
            app.UseSwagger();
            app.UseSwaggerUI(options =>
            {
                options.SwaggerEndpoint("/swagger/v1/swagger.json", "Support APP API");
            });
        }

        app.UseHttpsRedirection();
        app.UseRouting();

        app.UseAuditing();

        app.UseAuthorization();
        app.UseConfiguredEndpoints();
    }
}